package com.oro.domain;

import static javax.persistence.CascadeType.ALL;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@Entity
public class Sede implements Serializable {

	private static final long serialVersionUID = 1L;

	@ManyToMany(cascade = ALL)
	@JoinTable
	private List<ContattoScuola> contattiDiRiferimento;

	@ManyToMany(cascade = ALL)
	@JoinTable
	private List<Corso> corsi;

	@OneToMany(cascade = ALL)
	@JoinTable
	private List<Email> emails;

	@OneToMany(cascade = ALL)
	@JoinTable
	private List<Fax> faxes;

	@Id
	@GeneratedValue
	private Long id;

	@OneToOne(cascade = ALL)
	@JoinTable(name = "sede_indirizzo")
	// with {PERSIST, MERGE, REFRESH} the courses are not saved
	private Indirizzo indirizzo;// For extensibility, in case the business would
								// include USA or Canadian schools.

	@Column(unique = true)
	private String name;

	private String note;

	private boolean principale;

	@ManyToOne(cascade = ALL)
	@JoinTable(name = "sede_scuola")
	private Scuola school;

	@OneToMany(mappedBy = "sede", cascade = ALL)
	private List<ServizioAggiunto> serviziAggiunti;

	@OneToMany(cascade = ALL)
	@JoinTable
	private List<Telephone> telephones;

	public Sede() {
		super();
		indirizzo = new IndirizzoAU();
		setContattiDiRiferimento(new ArrayList<ContattoScuola>());
		/*
		 * As requested on 22/04/2011, when the new ADD SCHOOL form was
		 * submitted, A branch has zero one contact (which is compatible with
		 * the current model); but more branches can have the same contacts.
		 */
		getContattiDiRiferimento().add(null);
		setEmails(new ArrayList<Email>());
		setTelephones(new ArrayList<Telephone>());
		/*
		 * As requested on 22/04/2011, when the new ADD SCHOOL form was
		 * submitted, A branch has exactly one work phone.
		 */
		getTelephones().add(
				new Telephone("", Telephone.TYPE_WORK, Telephone.COUNTRY_AU));
		setFaxes(new ArrayList<Fax>());
		setCorsi(new ArrayList<Corso>());
	}

	public Sede(String name) {
		this();
		this.name = name;
	}

	public Sede(String name, Scuola school) {
		this(name);
		setSchool(school);
	}

	public boolean add(ContattoScuola cs) {
		contattiDiRiferimento.add(cs);
		return true;
	}

	public boolean add(Corso corso) {
		return corsi.add(corso);

	}

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o != null)
			if (o instanceof Sede)
				if (this.id != null)
					return this.id.equals(((Sede) o).getId());
				else
					return this.getIndirizzo()
							.equals(((Sede) o).getIndirizzo())
							&& this.getName().equals(((Sede) o).getName())
							&& this.getSchool().equals(((Sede) o).getSchool());
			else
				return false;
		else
			return false;

	}

	/*
	 * As requested on 22/04/2011, when the new ADD SCHOOL form was submitted, A
	 * branch has zero one contact (which is compatible with the current model);
	 * but more branches can have the same contacts.
	 */
	public ContattoScuola getContact() {
		return contattiDiRiferimento.get(0);
	}

	public List<ContattoScuola> getContattiDiRiferimento() {
		return contattiDiRiferimento;
	}

	public List<Corso> getCorsi() {
		return corsi;
	}

	public List<Email> getEmails() {
		return emails;
	}

	public List<Fax> getFaxes() {
		return faxes;
	}

	public Long getId() {
		return id;
	}

	public Indirizzo getIndirizzo() {
		return indirizzo;
	}

	public String getName() {
		return name;
	}

	public String getNote() {
		return note;
	}

	public Scuola getSchool() {
		return school;
	}

	public List<ServizioAggiunto> getServiziAggiunti() {
		return serviziAggiunti;
	}

	/*
	 * As requested on 22/04/2011, when the new ADD SCHOOL form was submitted, A
	 * branch has one woek phone.
	 */
	public Telephone getTelephone() {
		return getTelephones().get(0);
	}

	public List<Telephone> getTelephones() {
		return telephones;
	}

	public boolean isPrincipale() {
		return principale;
	}

	/*
	 * As requested on 22/04/2011, when the new ADD SCHOOL form was submitted, A
	 * branch has zero one contact (which is compatible with the current model);
	 * but more branches can have the same contacts.
	 */
	public void setContact(ContattoScuola cs) {
		contattiDiRiferimento.set(0, cs);
	}

	public void setContattiDiRiferimento(
			List<ContattoScuola> contattiDiRiferimento) {
		this.contattiDiRiferimento = contattiDiRiferimento;
	}

	public void setCorsi(List<Corso> corsi) {
		this.corsi = corsi;
	}

	public void setEmails(List<Email> emails) {
		this.emails = emails;
	}

	public void setFaxes(List<Fax> faxes) {
		this.faxes = faxes;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setIndirizzo(Indirizzo indirizzo) {
		this.indirizzo = indirizzo;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public void setPrincipale(boolean principale) {
		this.principale = principale;
	}

	public void setSchool(Scuola school) {
		this.school = school;
	}

	public void setServiziAggiunti(List<ServizioAggiunto> serviziAggiunti) {
		this.serviziAggiunti = serviziAggiunti;
	}

	/*
	 * As requested on 22/04/2011, when the new ADD SCHOOL form was submitted, A
	 * branch has one woek phone.
	 */
	public void setTelephone(Telephone t) {
		getTelephones().set(0, t);
	}

	public void setTelephones(List<Telephone> telephones) {
		this.telephones = telephones;
	}
	
	@Override
	public String toString() {
		if (getId() != null && getId() != 0L)
			return getId().toString();
		else
			return super.toString();
	}

}